//
//  ElectronContractViewController.m
//  阿帕
//
//  Created 阿帕 on 2020/1/4.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "SelectBankListVC.h"

#import "SelectBankCell.h"

@interface SelectBankListVC ()<UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate>

@property (strong , nonatomic)UITextField *textFiled;

@property (strong , nonatomic)UITableView *tableView;

@property (strong , nonatomic)NSMutableArray *dataArray;

@property (strong , nonatomic)UIImageView *foundImageView;

@property (nonatomic, assign) NSInteger pageNum;

@property (nonatomic, strong) NSString *searchStr;

@end

@implementation SelectBankListVC

static const CGFloat cellHeight = 60;

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _pageNum = 1;
    
    [self setupBaseView];
    
}

- (void)setupBaseView {
    
    self.navigationItem.title = _isChooseBank ? @"选择开户银行" : @"选择开户网点";
    
    self.view.backgroundColor = BackgroundColor;
    self.searchStr = @"";
    _textFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, ScreenW, 40)];
    _textFiled.font = font(14);
    _textFiled.text = @"";
    _textFiled.delegate = self;
    _textFiled.backgroundColor = [UIColor whiteColor];
    _textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textFiled.keyboardType = UIKeyboardTypeDefault;
    _textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_isChooseBank ? @"请填写银行名称" : @"请填写网点名称" attributes:@{NSForegroundColorAttributeName: Light_TextColor}];
    _textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textFiled.text = self.bankNameS;
    
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 40)];
    _textFiled.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 40)];
    _textFiled.leftViewMode = UITextFieldViewModeAlways;
    [_textFiled addSubview: leftView];
    
    UIImageView *searchImage = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenW-40, 10, 20, 20)];
    searchImage.userInteractionEnabled = YES;
    searchImage.image = [UIImage imageNamed:@"gcy_icon_search_siji"];
    _textFiled.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    _textFiled.rightViewMode = UITextFieldViewModeAlways;
    [_textFiled addSubview:searchImage];
    
    [searchImage nyl_addTapGes:^{
        _pageNum = 1;
        [self netDataRequest];
    }];
    
    [self.view addSubview:_textFiled];
    
    [self.view addSubview:self.tableView];
    
    [self netDataRequest];
}

- (void)netDataRequest {
    if (_isChooseBank) {
        [self allBankListRequest];
    } else {
        [self allBankOutletListRequest];
    }
}

- (void)allBankListRequest {
    WEAKSELF
    
    if (_pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    NSDictionary *parmas = @{
        @"type": @"bank",
        @"status": @"0",
        @"name": self.searchStr,
        @"token": [UserDefaultUtil valueForKey:USER_TOKEN],
        @"pageNum": [NSNumber numberWithInteger:_pageNum],
        @"pageSize": @"15",
    };
    
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@", [NSString stringWithFormat:@"%@/ntocc-basic-api/dict",[ReAddressDynamic shared].baseUrl]) withParameter:parmas withVC:self netName:@"开户行银行列表" success:^(id response, NSString *msg) {
        
        if ([response[@"records"] count] != 0) {
            [weakSelf.dataArray addObjectsFromArray:[SelectBankListModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];
            
            weakSelf.foundImageView.hidden = YES;
            weakSelf.tableView.hidden = NO;
            
        } else{
            if (_pageNum == 1) {
                weakSelf.foundImageView.hidden = NO;
                weakSelf.tableView.hidden = YES;
            }
            
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            
        }
        
        [weakSelf.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
    
}

- (void)allBankOutletListRequest {
    WEAKSELF
    
    if (_pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    NSDictionary *parmas = @{
        @"bankName": self.searchStr,
        @"bankCode": _bankCode,
        @"pageNum": [NSNumber numberWithInteger:_pageNum],
        @"pageSize": @"15",
    };
    
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@/ntocc-basic-api/bankCard/getBankAssociationNumber", [ReAddressDynamic shared].baseUrl) withParameter:parmas withVC:self netName:@"获取中台银行网点" success:^(id response, NSString *msg) {
        
        if ([response[@"records"] count] != 0) {
            [weakSelf.dataArray addObjectsFromArray:[SelectBankOutletListModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];
            
            weakSelf.foundImageView.hidden = YES;
            weakSelf.tableView.hidden = NO;
            
        } else{
            if (_pageNum == 1) {
                weakSelf.foundImageView.hidden = NO;
                weakSelf.tableView.hidden = YES;
            }
            
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            
        }
        
        [weakSelf.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
    
}

//上拉刷新数据
- (void)loadNewData{
    _pageNum = 1;
    [self netDataRequest];
}
- (void)loadMoreData{
    _pageNum ++;
    [self netDataRequest];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString * str = [textField.text stringByReplacingCharactersInRange:range withString:string];
    self.searchStr = str;
    [self loadNewData];
    return YES;
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SelectBankCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (_isChooseBank) {
        SelectBankListModel *model = self.dataArray[indexPath.row];
        cell.model = model;
    } else {
        SelectBankOutletListModel *outletModel = self.dataArray[indexPath.row];
        cell.outletModel = outletModel;
    }
    
    return cell;
}



#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_isChooseBank) {
        if (self.getBankInfoBlock) {
            self.getBankInfoBlock(self.dataArray[indexPath.row]);
        }
    } else {
        if (self.getBankOutletInfoBlock) {
            self.getBankOutletInfoBlock(self.dataArray[indexPath.row]);
        }
    }
    
    [self.navigationController popViewControllerAnimated:YES];

}

#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 50, ScreenW, ScreenH - NavHeight - 50);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SelectBankCell class]) bundle:nil] forCellReuseIdentifier:@"CellID"];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (UIImageView *)foundImageView {
    if (!_foundImageView) {
        _foundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
        _foundImageView.image = image(@"gcy_img_list_found");
        _foundImageView.contentMode = UIViewContentModeScaleAspectFit;
        _foundImageView.center = self.view.center;
        _foundImageView.cy_centerY -=50;
        [self.view addSubview:_foundImageView];
    }
    return _foundImageView;
}

@end
